package work20241126_27;

import java.util.Arrays;

/**
 * @author zdl
 * @date 2024/11/27
 */
public class Demo34 {
    public static void main(String[] args) {
        //返回两个数组的相似度（交集元素与并集元素的比值）。
        int[] arr = {1,3,2,1,2};
        int[] new_arr = duplicate(arr);
        System.out.println(Arrays.toString(new_arr));
        int[] arr1 = {9,1,1,2};
        int[] new_arr1 = duplicate(arr1);
        System.out.println(Arrays.toString(new_arr1));
        double rate = rate(new_arr,new_arr1);
        System.out.println(rate);
    }

    //去重
    public static int[] duplicate(int[] arr){
        int len = arr.length;
        /**
         * 执行流程:  arr={9,1,1,2}  len = 4
         *   i=0 0<4
         *      j=1 1<4 true if(arr[0] == arr[1]) false j++
         *      j=2 2<4 true if(arr[0] == arr[2]) false j++
         *      j=3 3<4 true if(arr[0] == arr[3]) false j++
         *      j=4 4<4 false
         *  i=1 1<4
         *      j=2 2<4 true if(arr[1] == arr[2]) true  arr[2] = arr[3] ===>{9,1,2,2} len-- =3 j-- = 1  j++ = 2
         *      j=2 2<3 if(arr[1] == arr[2]) false j++
         *      j=3 3 < 3 false
         *  i=2 2<3
         *      j=3 3<3 false
         *  i=3 3<3 false
         *
         */
        for (int i = 0; i < len; i++) {
            for (int j = i+1; j < len; j++) {
                if (arr[i] == arr[j]){
                    arr[j] = arr[len - 1];
                    len--;
                    j--;
                }
            }
        }
        int[] newArr = new int[len];
        for (int i = 0; i < newArr.length; i++) {
            newArr[i] = arr[i];
        }

        return newArr;
    }

    //交集元素与并集元素的比值
    public static double rate(int[] arr , int[] arr1){
//        int[] arr = {1,3,2,1,2};
//        int[] arr1 = {9,8,1,2};
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr1.length; j++) {
                if (arr[i] == arr1[j]){
                    count++;
                }
            }
        }
        return count *1.0 /( arr.length+ arr1.length-count) ;
    }
}
